package com.example.springbootblog.service;


import com.example.springbootblog.mapper.BlogInfoMapper;
import com.example.springbootblog.model.BlogInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BlogService {
    @Autowired
    BlogInfoMapper blogInfoMapper;
    public List<BlogInfo> getBlogList() {
        List<BlogInfo> blogInfos = blogInfoMapper.getBlogList();
        for (BlogInfo blogInfo: blogInfos) {
            log.info("修改正文: " + blogInfo.getContent());
            String content = blogInfo.getContent();
            content = content.replace("##","");
            if(content.length() >= 50) {
                // 进行截断
                content = content.substring(0,50) + "...";
            }
            blogInfo.setContent(content);
        }

        return blogInfos;
    }

    public BlogInfo getBlogById(Integer blogId) {
        return blogInfoMapper.getBlogById(blogId);
    }

    public Integer add(BlogInfo blogInfo) {
        return blogInfoMapper.inserBlog(blogInfo);
    }

    public Integer updateBlog(BlogInfo blogInfo) {
        return  blogInfoMapper.updateBlog(blogInfo);
    }

    public Integer deleteBlod(Integer blogId) {
        return blogInfoMapper.deleteBlogById(blogId);
    }
}
